<?php


class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    protected function _initAppAutoload() {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'App',
            'basePath' => dirname(__FILE__),
        ));
        return $autoloader;
    }

    protected function _initRouter() {
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/router.ini', APPLICATION_ENV);
        $route = new Zend_Controller_Router_Rewrite();
        $route->addConfig($config, 'routes');
        
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $router = $frontController->getRouter();
        $router->addRoutes($route->getRoutes());
        
        $frontController->setParam('useDefaultControllerAlways',true);
    }

    protected function _initCache() {
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/cache.ini', APPLICATION_ENV);
        
        $frontend = $config->frontend->name;
        $frontendOptions = $config->frontend->options->toArray();

        $backend = $config->backend->name;
        $backendOptions = $config->backend->options->toArray();

        $cache = Zend_Cache::factory($frontend, $backend, $frontendOptions, $backendOptions);
        Zend_Registry::set('Zend_Cache', $cache);
    }
    
    protected function _initSession()
    {
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $frontController->registerPlugin(new Ext_Controller_Plugin_Session());
    }
    
    protected function _initDatabase()
    {
        $databases = new Zend_Config_Ini(APPLICATION_PATH . '/configs/database.ini', APPLICATION_ENV);
                
        $config = array();
        $config['driver_options'] = array('PDO::ATTR_TIMEOUT'=>5);
        foreach($databases->database as $k => $v){
            if($k == 'servers'){
                foreach($v as $k2 => $v2){
                    foreach($v2 as $k3 => $v3){
                        $config[$k][$k3][$k2] = $v3;
                    }
                }
            }else{
                $config[$k] = $v;
            }
        }
        //print_r($config);
        Zend_Registry::set('dbprefix', $config['dbprefix']);
        
        $this->bootstrap('cache');
        $cache = Zend_Registry::get('Zend_Cache');
        $dataSource = new Zf_Orm_DataSource($config, $cache, 'cache_tag');
        Zend_Registry::set('dataSource', $dataSource);
        
        $_use_adapter = "slave";
        $db = Zend_Registry::get('dataSource')->getConnection($_use_adapter);
        Zend_Db_Table::setDefaultAdapter($db);
        Zend_Registry::set('_use_adapter', $_use_adapter);
        
        Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
    }
    
    protected function _initLayout() {
        $layout = Zend_Layout::startMvc();
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $frontController->registerPlugin(new Ext_Controller_Plugin_Layout()); 
    }
    
    protected function _initSetting() {
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $frontController->registerPlugin(new Ext_Controller_Plugin_Setting());
    }
    
    /*
    protected function _initExtHelper() {
        $this->bootstrap('frontController'); 
        Zend_Controller_Action_HelperBroker::addHelper(new Ext_Controller_Action_Helper_Acl()); 
    }*/
    
    protected function _initLocal() {
        $locale = new Zend_Locale("zh_CN");

        $translate = new Zend_Translate(
            'gettext', //适配器
            ROOT_PATH.'/languages', //语言包文件夹，mo文件存放位置
            $locale, //Zend_Locale对象
            array('scan' => Zend_Translate::LOCALE_FILENAME)//自动扫描形式
        );
        Zend_Registry::set('Zend_Translate', $translate);
    }
        
    protected function _initDebug() {
        /*
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $frontController->registerPlugin(new Ext_Controller_Plugin_Debug());
        */
        
        $options = $this->getOptions();

        if ($options['zfdebug']['open'])
        {
            $autoloader = Zend_Loader_Autoloader::getInstance();
            $autoloader->registerNamespace('ZFDebug');
            
            $options = array(
                'plugins' => array('Variables', 
                                   'File'=>array('base_path'=>realpath(APPLICATION_PATH.'/../')),
                                   'Memory', 
                                   'Time', 
                                   'Registry', 
                                   'Exception')
            );
            /*
            # Instantiate the database adapter and setup the plugin.
            # Alternatively just add the plugin like above and rely on the autodiscovery feature.
            if (Zend_Registry::isRegistered('_use_adapter')) {
                //$bootstrap->bootstrap('db');
                //$db = $bootstrap->getPluginResource('db')->getDbAdapter();
                
                $_use_adapter = Zend_Registry::get('_use_adapter');
                $db = Zend_Registry::get('dataSource')->getConnection($_use_adapter);
                $options['plugins']['Database']['adapter'] = $db;
            }*/
            
            $_use_adapter = "slave";
            $db = Zend_Registry::get('dataSource')->getConnection($_use_adapter);
            $options['plugins']['Database']['adapter'][] = $db;
            
            $_use_adapter = "master";
            $db = Zend_Registry::get('dataSource')->getConnection($_use_adapter);
            $options['plugins']['Database']['adapter'][] = $db;
            
            if ($this->hasPluginResource('cache')) {
                $cache = $this->getPluginResource('cache')->getDbAdapter();
                $options['plugins']['Cache']['backend'] = $cache->getBackend();
            }
            
            $debug = new ZFDebug_Controller_Plugin_Debug($options);
            
            $this->bootstrap('frontController');
            $frontController = $this->getResource('frontController');
            $frontController->registerPlugin($debug);
        }
    }
}